package coop.bancocredicoop.proyectos.gd

import coop.bancocredicoop.proyectos.gd.Document
import coop.bancocredicoop.proyectos.gd.Customer
import coop.bancocredicoop.proyectos.gd.Box
import coop.bancocredicoop.proyectos.gd.DocumentType

import coop.bancocredicoop.proyectos.gd.DocumentsCSVWriter

class DocumentsCSVWriterTests extends GroovyTestCase {

    def aDocumentType() {
        def fileType = new FileType(shortName: 'Excepcion', 
                                    name: 'Excepción')
        new DocumentType(
            belongsTo: fileType,
            shortName: 'testingType',
            name: 'Testing Type',
            versionable: false,
            purgeable: true,
            hasExpiration: true,
            daysInAdvanceForNotice: 7,
            defaultLastingMonths: 12,
            uploadNotificationEmailAddress: 'testing@example.com')

    }

    def aDocument() {
        def customer = new Customer(idPersona: '12345',
                                    name: 'Testing Customer',
                                    cuit: '20-33333333-1')
        def box = new Box(cajaId: '1234')
        def documentType = aDocumentType()
        def documentProperties = [
            name: 'testing Document',
            customer: customer,
            box: box,
            tags: "",
            documentVersion: '1.0',
            realVersion: 1,
            lastVersion: true,
            validTo: getDate(2015, 1, 1),
            documentType: documentType,
            notes: 'testing notes',
            createdDate: getDate(2014, 4, 4),
            lastUpdated: getDate(2014, 4, 4),
            deleteDate: null,
            deleteByUserName: null
        ]                
        def document = new Document(documentProperties)
        document

    }

    void testEmptyListOfDocumentsGivesEmptyOutput() {
        def outputStream = new ByteArrayOutputStream()
        def csvWriter = new DocumentsCSVWriter(outputStream)
        csvWriter.write([])
    
        assertEquals "", outputStream.toString()
    }
    
    void testWriteACSVWithTwoDocuments() {
        def outputStream = new ByteArrayOutputStream()
        def csvWriter = new DocumentsCSVWriter(outputStream)
        csvWriter.write([aDocument(), aDocument()])
        def expectedOutput = new StringBuilder()
             .append('"idPersona","Denominacion","CUIT","Tipo de documento","Caja AdeA","Fecha de carga","Version"\n')
             .append('"12345","Testing Customer","20-33333333-1","Testing Type","1234","2014-04-04 00:00:00","1"\n')
             .append('"12345","Testing Customer","20-33333333-1","Testing Type","1234","2014-04-04 00:00:00","1"')
             .toString()
        assertEquals expectedOutput, outputStream.toString() 
    }
    
    private Date getDate(year, month, day) {
        Calendar cal = GregorianCalendar.getInstance()
        cal.set(year, month-1, day)
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.getTime()
    }


}
